package com.phone.servlet.user;

import com.phone.SMSCode;
import com.phone.SendCode;
import com.phone.entity.Result;
import com.phone.servlet.BaseServlet;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Package name com.phone.servlet.user
 * Created by tisen
 * On 2017/7/26. in 10:22
 */
public class Send extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String phone = request.getParameter("phone");
        Result<String> result = new Result<>();
        result.setResult("error");
        result.setObject(null);
        try {
            String code = SMSCode.getSMSCode();
            String status = SendCode.sendTextSms(phone, code);
            if (status.equals("0")) {
                result.setReason(status);
                result.setResult("successful");
                result.setObject(code);
            } else {
                result.setReason(status);
            }

        } catch (Exception e) {
            result.setReason("-1");
            e.printStackTrace();
        } finally {
            out(response, result);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
